﻿using ConfExpert.Forms.Base;

using DevTeam;

using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace ConfExpert.Forms.Depo.Aksesuar
{
    public partial class SonHareket : XForm
    {
        public SonHareket()
        {
            InitializeComponent();
        }

        BindingSource bs;
        private void SonHareket_Load(object sender, EventArgs e)
        {
            
            DataSet d = ExecuteDataSet("GetAgesAksesuar");
            DataTable dt = d.Tables[0]; //.Copy();
            DataTable resDt = new DataTable();
            resDt.Columns.Add("AksesuarId", typeof(int));
            resDt.Columns.Add("Stok", typeof(int));
            resDt.Columns.Add("Months", typeof(float));
            resDt.Columns.Add("Brand", typeof(string));
            resDt.Columns.Add("SonHareket", typeof(DateTime));

            bool flag = false;
            string brand = string.Empty;
            DateTime sonhareket = new DateTime(2000, 1, 1);
            int AksesuarId = 0, gAdetInc = 0, stok = 0;
            float resultingAge = 0, ageT = 0;
            foreach (DataRow dr in dt.Rows)
            {
                if (AksesuarId != dr["AksesuarId"].ToInt(-1))
                {
                    if (AksesuarId > 0)
                        resDt.Rows.Add(AksesuarId, stok, resultingAge * 12.0, brand, sonhareket);

                    AksesuarId = dr["AksesuarId"].ToInt(-1);
                    gAdetInc = 0;
                    ageT = 0;
                    sonhareket = new DateTime(2000, 1, 1);
                    brand = string.Empty;
                    resultingAge = 0f;
                    flag = false;
                }

                sonhareket = datetimeParse(dr["SonHareket"]);
                brand = dr["Brand"].ToString();
                stok = dr["Stok"].ToInt(-1);
                if (stok != 0)
                {
                    int gAdet = dr["GelenAdet"].ToInt(-1);
                    float ageY = (float)dr["Age"].ToDecimal(-1);
                    gAdetInc += gAdet;


                    if (!flag)
                    {
                        if (gAdetInc < stok)
                            ageT += gAdet * ageY;
                        else
                        {
                            ageT += (stok - (gAdetInc - gAdet)) * ageY;
                            flag = true;
                        }
                    }

                    resultingAge = ageT / stok;
                }
            }

            resDt.Rows.Add(AksesuarId, stok, resultingAge * 12.0, brand, sonhareket);
            bs = gvAksesuarAge.Init(resDt);
            gvAksesuarAge.AddColumn("Agirlik", typeof(decimal), "Stok*Months/1e5");
            gvAksesuarAge.Columns["Agirlik"].ToolTip = "StokMiktarı * Yaş(ay) / 100.000";
            gvAksesuarAge.MakeRowSelectable();
            gvAksesuarAge.MakeReadOnly();

            gvAksesuarAge.FormatAsDate("SonHareket");
            gvAksesuarAge.FormatAsNumber(0, "Months", "Stok");
            gvAksesuarAge.FormatAsNumber(2, "Agirlik");
            gvAksesuarAge.AddSummary("Stok");
            gvAksesuarAge.SetCaptions("Months:Yaş (Ay Cinsinden)");
            gvAksesuarAge.YanYanaGetir("Brand:ProductCategoryId");
            gvAksesuarAge.YanYanaGetirSoluna("BrandId", "AksesuarId", "Months", "Stok", "SonHareket", "Agirlik");
            gvAksesuarAge.RowCellStyle += gvAksesuarAge_RowCellStyle;
            gvAksesuarAge.SortInfo.Add(gvAksesuarAge.Columns["Agirlik"], DevExpress.Data.ColumnSortOrder.Descending);

            gvAksesuarAge.BestFitColumns();

            textEdit1.EditValueChanged += textEdit1_EditValueChanged;

            GridManager gm = GridManager.GetManager(gvAksesuarAge);
            gm.GridMenu.AddMenu("Stok Hareketleri", StokHareketleri);
        }


        void StokHareketleri(object sender, EventArgs e)
        {
            int handle = gvAksesuarAge.FocusedRowHandle;

            if (handle > -1)
            {
                int AksesuarId = intParse(gvAksesuarAge.GetRowCellValue(handle, "AksesuarId"));
                using (AksesuarHareketGC ah = new AksesuarHareketGC(AksesuarId, null, null))
                    ah.ShowDialog();
            }
        }

        void textEdit1_EditValueChanged(object sender, EventArgs e)
        {
            int Ay = intParse(textEdit1.EditValue);
            if (Ay == 0)
                bs.Filter = string.Empty;
            else
                bs.Filter = "Months>=" + Ay.ToString();
        }

        void gvAksesuarAge_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
        {
            if (e.RowHandle > -1)
                if (e.Column.FieldName == "AksesuarId" || e.Column.FieldName == "Stok")
                    e.Appearance.Font = new Font(DevExpress.Utils.AppearanceObject.DefaultFont, FontStyle.Bold);
        }
    }
}
